#include "student_.h"
#include "ui_student_.h"

student_::student_(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::student_)
{
    ui->setupUi(this);
    setWindowFlags(Qt::Dialog);
    setFixedSize(575, 311);
}

student_::~student_()
{
    delete ui;
}

void student_::set_sno(QString a)
{
    sno=a;
}

void student_::initialise()  //显示学生选课信息
{
    QSqlQuery query1,query2,query3;
    query1.exec("select count(*) from s_c where sno='"+sno+"'");  //从选课表查询当前学生的总选课数
    query1.next();
    int size=query1.value(0).toInt();
    QString course[size];    //用以已选过的课程名称存储
    query2.exec("select cname from course where cno in(select cno from s_c where sno='"+sno+"')");  //查询该学生所选的课程名称列表
    query2.next();
    for(int i=0;i<size;i++)
    {
        course[i]=query2.value(0).toString();
        query2.next();
        ui->course->setText(ui->course->text().append("     "+course[i]+"\n"));  //将课程名称显示在界面对应位置
    }

    query3.exec("select cname,captive,surplus,cno from course where college in(select college from student where sno='"+sno+"')");
    //查询当前学生所在学院所开设的课程
    while(query3.next())
    {
        QString cname,temp;
        cname=query3.value(0).toString();
        bool labe=true;
        for(int i=0;i<size;i++)  //遍历course查询当前课程是否已经被选过
        {
            if(cname==course[i])
                labe=false;
        }
        if(labe)  //此课程还未被该同学选过
        {
            QCheckBox* check = new QCheckBox();  //声明一个复选框
            temp=query3.value(3).toString()+"   "+cname+"   "+query3.value(1).toString()+"   "+query3.value(2).toString();
            check->setText(temp);   //在复选框显示该课程基本信息
            ui->selected->addWidget(check);  //将复选框加到界面里
            vector_all.push_back(check);     //将复选框入队
            vector_name.push_back(cname);    //将课程名字入队
        }

    }


}

void student_::on_pushButton_clicked()  //select按钮点击触发
{
    int size;
    size = vector_all.size();  //获取队列大小
    QSqlQuery query,query1;
    for(int i=0;i<size;i++)
    {
        if(vector_all[i]->checkState())  //判断复选框是否被选中
        {
            vector_all[i]->setCheckState(Qt::Unchecked);
            vector_all[i]->setText("");
            query1.exec("select cno from course where cname='"+vector_name[i]+"'");   //根据对应课程名查询课程号
            query1.next();
            QString cno=query1.value(0).toString();
            query.exec("insert into s_c values('"+sno+"','"+cno+"')");  //将学号与课程号插入选课表
            ui->selected->removeWidget(vector_all[i]);  //从界面中将本复选框删除
            ui->course->setText(ui->course->text().append("     "+vector_name[i]+"\n"));  //将课程名加到已选课程后
        }
    }
    vector_all.clear();  //清空队列
    vector_name.clear();
}

void student_::on_actionalter_triggered()  //修改密码点击触发
{
    alt.set_type(0);
    alt.set_number(sno);
    alt.show();
    this->close();
}
